package com.ruoyi.web.controller.member;

import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.yijing.domain.CustomerInfo;
import com.ruoyi.yijing.domain.CustomerInfoAccount;
import com.ruoyi.yijing.domain.CustomerInfoAccountToken;
import com.ruoyi.yijing.service.ICustomerInfoAccountService;
import com.ruoyi.yijing.service.ICustomerInfoAccountTokenService;
import com.ruoyi.yijing.service.ICustomerInfoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.http.HttpServletRequest;

public class Base {

    protected final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    protected ICustomerInfoAccountService customerInfoAccountService;

    @Autowired
    protected ICustomerInfoAccountTokenService customerInfoAccountTokenService;

    @Autowired
    private ICustomerInfoService customerInfoService;


    public static String getMemberToken()
    {
        HttpServletRequest request = ServletUtils.getRequest();
        if (request != null){
            return request.getHeader("customerToken");
        }
        return "";
    }

    public static String getAppId()
    {
        HttpServletRequest request = ServletUtils.getRequest();
        if (request != null){
            return request.getHeader("appId");
        }
        return "";
    }

    protected CustomerInfo getMemberInfo(){
        CustomerInfoAccountToken token = customerInfoAccountService.getToken(getMemberToken());
        if (token != null){
            if (token.getCustomerUid() != null) {
                CustomerInfo ci = customerInfoService.selectCustomerInfoByCustomerUid(token.getCustomerUid());
                if (ci != null) {
                    return ci;
                }
            }
        }
        return null;
    }

    protected CustomerInfoAccount getMemberAccount(){
        CustomerInfoAccountToken token = customerInfoAccountService.getToken(getMemberToken());
        if (token != null){
            CustomerInfoAccount account = customerInfoAccountService.selectCustomerInfoAccountByCustomerAccountUid(token.getCustomerAccountUid());
            if (account != null){
                return account;
            }
        }
        return null;
    }

    protected CustomerInfoAccountToken getMemberAccountToken(){
        CustomerInfoAccountToken token = customerInfoAccountService.getToken(getMemberToken());
        if (token != null){
            return token;
        }
        return null;
    }

    /**
     * 设置请求分页数据
     */
    protected void startPage()
    {
        PageUtils.startPage();
    }
}
